/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lczyfy.idcp.modules.sys.vueweb;

import com.lczyfy.idcp.common.config.Global;
import com.lczyfy.idcp.common.persistence.Page;
import com.lczyfy.idcp.common.utils.ResponseCode;
import com.lczyfy.idcp.common.utils.ServerResponse;
import com.lczyfy.idcp.common.web.BaseController;
import com.lczyfy.idcp.modules.sys.entity.Dict;
import com.lczyfy.idcp.modules.sys.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典Controller
 * @author ThinkGem
 * @version 2014-05-16
 */
@Api(value = "dict")
@RestController
@RequestMapping(value = "${apiPath}/sys/dict")
public class DictVueController extends BaseController {

	@Autowired
	private DictService dictService;
	@ApiOperation(value = "获取字典列表",notes = "")
	@RequiresPermissions("sys:dict:view")
	@RequestMapping(value = "search",method = RequestMethod.POST) //查询日记
	public ServerResponse<Dict> search(@RequestBody Dict dict, int pageNo, int pageSize) {
		Page<Dict> page =  dictService.findPage(new Page<Dict>(pageNo,pageSize),dict);
		return new ServerResponse(ResponseCode.SUCCESS.getCode(), page);
	}
    @ApiOperation(value = "添加和更新字典",notes = "添加字典")
	@RequiresPermissions("sys:dict:edit")
    @RequestMapping(value = "save",method = RequestMethod.POST) //菜单列表
    public ServerResponse<String> create(@RequestBody Dict dict) {
		if(Global.isDemoMode()){
            return  ServerResponse.createByErrorMessage("演示模式，不允许操作！");
		}
		dictService.save(dict);
		return  ServerResponse.createBySuccessMessage("保存字典'" + dict.getLabel() + "'成功");
    }
	@ApiOperation(value = "通过字典id详情",notes = "获取详情")
	@RequiresPermissions("sys:dict:edit")
	@RequestMapping(value = "findById/{id}",method = RequestMethod.GET) //菜单列表
	public ServerResponse<Dict> findById(@PathVariable String id) {
		if(Global.isDemoMode()){
			return  ServerResponse.createByErrorMessage("演示模式，不允许操作！");
		}
		Dict findByDict = dictService.get(id);
		return  new ServerResponse(ResponseCode.SUCCESS.getCode(),findByDict) ;
	}
    @ApiOperation(value = "删除字典",notes = "删除字典")
	@RequiresPermissions("sys:dict:edit")
    @RequestMapping(value = "delete",method = RequestMethod.DELETE) //菜单列表
    public ServerResponse<String> delete(@RequestBody Dict dict) {
        if(Global.isDemoMode()){
            return  ServerResponse.createByErrorMessage("演示模式，不允许操作！");
        }
        dictService.delete(dict);
        Dict findByDict = dictService.get(dict.getId());
        return  ServerResponse.deleteBySuccessMessage("删除字典'" + findByDict.getLabel() + "'成功");
    }
	@ApiOperation(value = "获取所有的字典接口",notes = "所有字典类型接口")
	@RequiresPermissions("sys:dict:view")
	@RequestMapping(value = "allType",method = RequestMethod.GET) //菜单列表
	public ServerResponse<String> dictType() {
		List<String> typeList = dictService.findTypeList();
		return new ServerResponse<String>(typeList);
	}
	@ApiOperation(value = "通过字典类型获取接口",notes = "字典类型")
	@RequiresPermissions("sys:dict:view")
	@RequestMapping(value = "listData",method = RequestMethod.GET)
	public List<Dict> listData(String type) {
		Dict dict = new Dict();
		dict.setType(type);
		return dictService.findList(dict);
	}

}
